﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using ESRI.ArcGIS.Geodatabase;

namespace EAGL.Data
{
    public static class FieldMapper
    {
        /// <summary>
        /// Creates a dictionary that contains a list of names with their corresponding Field objects indexes
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="fieldNames"></param>
        /// <returns></returns>
        public static Dictionary<string, int> GetMappings(IFields fields, List<string> fieldNames)
        {
            Dictionary<string, int> mappings = new Dictionary<string, int>();
            int c = fieldNames.Count;
            int i = 0;
            string s = null;
            int x = 0;
            while (i < c)
            {
                s = fieldNames[i];
                x = fields.FindField(s);
                mappings.Add(s, x);
                i++;
            }
            return mappings;  
        }

        /// <summary>
        /// Creates a dictionary that has index values form first field list as keys and mapped indexes
        /// from the second fiels list as values.
        /// </summary>
        /// <param name="srcFields"></param>
        /// <param name="desFields"></param>
        /// <returns></returns>
        public static Dictionary<short, short> GetMappings(IFields srcFields, IFields desFields)
        {
            Dictionary<short, short> mapping = new Dictionary<short, short>();
            short c = (short)srcFields.FieldCount;
            short i = 0;

            while (i < c)
            {
                short index = (short)desFields.FindField(srcFields.get_Field(i).Name);
                if (index >= 0)
                    mapping.Add(i, index);
                i++;
            }
            return mapping;
        }

    }
}
